package org.mikrosopht.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.json.client.JSONException;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MouseListenerAdapter;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class MovieVote implements EntryPoint {

  private MovieList movieList = new MovieList();

  /**
   * This is the entry point method.
   */
  public void onModuleLoad() {
    RootPanel movieListSlot = RootPanel.get("movie_list");
    movieListSlot.add(movieList);
    movieList.rightFrame.setUrl(MovieVoteConstants.INITIAL_RIGHT_FRAME_URL);

    // Retrieve user info and then update movie list.
    String url = URL.encode(MovieVoteConstants.USER_INFO_JSON_URL);
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
    try {
      builder.sendRequest(null, new UserInfoHandler());
    } catch (RequestException e) {
      Window.alert("Couldn't retrieve JSON: " + e);
    }
  }

  public static void redirectToLogin() {
    Window.open(MovieVoteConstants.LOGIN_URL, "_self", null);
  }

  private class UserInfoHandler implements RequestCallback {
    public void onError(Request request, Throwable exception) {
      Window.alert("Couldn't retrieve JSON: " + exception);
    }
    public void onResponseReceived(Request request, Response response) {
      if (response.getStatusCode() == 200) {
        try {
          JSONObject object = JSONParser.parse(response.getText()).isObject();
          String sort1 = object.get("sort1").isString().stringValue();
          String sort2 = object.get("sort2").isString().stringValue();
          String weighting = object.get("weighting_description").isString().stringValue();
          String username = object.get("username").isString().stringValue();
          movieList.setSorting(sort1, sort2);
          Label weightingLabel = Label.wrap(DOM.getElementById("weighting_description"));
          weightingLabel.setText(weighting);
          weightingLabel.addMouseListener(new WeightingListener());
          Label usernameLabel = Label.wrap(DOM.getElementById("username"));
          usernameLabel.setText(username);
          usernameLabel.addMouseListener(new WeightingListener());
        } catch (JSONException e) {
          redirectToLogin();
        }
      } else {
        Window.alert("Couldn't retrieve JSON: " + response.getStatusText());
      } 
      movieList.populateMovieList(true);
    }
  }

  private class WeightingListener extends MouseListenerAdapter {
    public void onMouseEnter(Widget sender) {
      movieList.rightFrame.setUrl(MovieVoteConstants.USER_STATS_URL);
    }
  }
}
